package File_analyze;

import java.text.SimpleDateFormat;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

public class independent_analyze {

	private String fileDetails;
	private BufferedReader br = null;
	private String s = null;
	private Integer code = 0;
	private Integer codeComments = 0;
	private Integer codeBlank = 0;
	
	public String getFileDetails() {
		return fileDetails;
	}

	public void setFileDetails(String fileDetails) {
		this.fileDetails = fileDetails;
	}

	public BufferedReader getBr() {
		return br;
	}

	public void setBr(BufferedReader br) {
		this.br = br;
	}

	public String getS() {
		return s;
	}

	public void setS(String s) {
		this.s = s;
	}

	public  Integer getCode() {
		return code;
	}

	

	public  Integer getCodeComments() {
		return codeComments;
	}

	

	public  Integer getCodeBlank() {
		return codeBlank;
	}

	

	public String parse(File file,int Flag) {

		StringBuilder builder = new StringBuilder();
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

		String time = sdf.format(new Date(file.lastModified()));
		
		try {
			br = new BufferedReader(new FileReader(file));
			boolean comm = false;
			while ((s = br.readLine()) != null) {
				if (s.startsWith("/*") && s.endsWith("*/")) {
					codeComments++;
				} else if (s.trim().startsWith("//")) {
					codeComments++;
				} else if (s.startsWith("/*") && !s.endsWith("*/")) {
					codeComments++;
					comm = true;
				} else if (!s.startsWith("/*") && s.endsWith("*/")) {
					codeComments++;
					comm = false;
				} else if (comm) {
					codeComments++;
				} else if (s.trim().length() < 1) {
					codeBlank++;
				} else {
					code++;
				}
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(Flag == 1) {
			builder.append(String.format("    --"+"%-35s          Total: %5d     Blank: %5d %6d bytes",
				file.getName(), code+codeBlank+codeComments ,codeBlank, file.length()));
			builder.append('\n');
		}
		else 
		{
			builder.append(String.format("        --"+"%-35s      Total: %5d     Blank: %5d %6d bytes",
					file.getName(), code+codeBlank+codeComments ,codeBlank, file.length()));
			builder.append('\n');
		}
		
		this.fileDetails = builder.toString();
		
		code = 0;
		codeBlank = 0;
		codeComments = 0;
		
		return this.fileDetails;
	
		
}
	}
//	public String getFileDetails() {
//		return fileDetails;
//	}


